package {
	import com.tenoz.display.DeathParticle;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class ParticleFun extends Sprite
	{
		public var pool:Array = [];
		public var active:Array = [];
		
		public function ParticleFun()
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		protected function onMove(e:MouseEvent):void
		{
			var particle:DeathParticle;
			
			if(pool.length > 0)
			{
				particle = pool.pop() as DeathParticle;
				particle.recycle(this.mouseX, this.mouseY);
			}
			else
			{
				particle = new DeathParticle(this.mouseX, this.mouseY);
				particle.addEventListener('dead', addToPool);
			}
			this.addChild(particle);
			active.push(particle);
			trace("ACTIVE:"+active.length+" POOLED:"+pool.length);
		}
		
		protected function addToPool(e:Event):void
		{
			var particle:DeathParticle = e.target as DeathParticle;
			this.removeChild(particle);
			pool.push(particle);
			active.splice(active.indexOf(particle),1);
		}
	}
}